//
//  ZyaPayListCell.swift
//  SlaughterClient
//
//  Created by 邱仙凯 on 2021/3/23.
//  Copyright © 2021 zwb. All rights reserved.
//

import UIKit
 
class ZyaPayListCell: UITableViewCell {
    
    var deleteEvent: (() -> Void)?
    
    var siliaoModel: ZyaFeedOrderList? {
        didSet {
            noLabel.text = siliaoModel?.sOrderformNo
            left1.text = siliaoModel!.sOrderformDate.unwrap
            left2.text = siliaoModel!.zPayStatus.unwrap == "1" ? "已付款" : "未付款"
            right1.text = "业务员：" + siliaoModel!.sOperationNm.unwrap
            right2.text = "销管已确认"
            deleteButton.isHidden = true
        }
    }
    var payModel: ZyaFeedOrderList? {
        didSet {
            noLabel.text = payModel?.sNo
            left1.text = payModel!.sDate.unwrap
            left2.text = "金额：" +  payModel!.sPayMoney.unwrap
            right1.text =  "业务员：" + payModel!.sOperationNm.unwrap
            right2.text = payModel!.sPayState.unwrap == "1" ? "已付款" : "未付款"
            if payModel!.sPayState.unwrap == "1" {
                deleteButton.isHidden = true
            } else {
                deleteButton.isHidden = false
            }
            
        }
    }

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }
    
    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
        
        // Configure the view for the selected state
    }
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        backgroundColor = Theme.backgroudColor
        selectionStyle = .none
        
        addSubview(bgView)
        bgView.addSubview(subview)
        bgView.addSubview(left1)
        bgView.addSubview(noLabel)
        bgView.addSubview(right1)
        bgView.addSubview(left2)
        bgView.addSubview(right2)
        bgView.addSubview(deleteButton)
        
        setupConstraints()
        
        deleteButton.addTarget(self, action: #selector(deleteAction), for: .touchUpInside)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    @objc func deleteAction() {
        if let action = deleteEvent {
            action()
        }
    }
    
    private func setupConstraints () {
        
        bgView.snp.makeConstraints { (maker) in
            maker.edges.equalTo(UIEdgeInsets(top: 4, left: 8, bottom: 4, right: 8))
        }
        
        subview.snp.makeConstraints { (maker) in
            maker.right.left.top.equalToSuperview()
            maker.height.equalTo(30)
        }
        
        deleteButton.snp.makeConstraints { (maker) in
            maker.trailing.equalTo(subview.snp.trailing).inset(8)
            maker.size.equalTo(CGSize(width: 25, height: 25))
            maker.centerY.equalTo(subview)
        }
        
        noLabel.snp.makeConstraints { (maker) in
            maker.left.equalTo(8)
            maker.centerY.equalTo(subview.snp.centerY)
        }
        
        left1.snp.makeConstraints { (maker) in
            maker.left.equalTo(8)
            maker.top.equalTo(noLabel.snp.bottom).offset(16)
        }
        
        right1.snp.makeConstraints { (maker) in
            maker.right.equalTo(bgView).offset(-8)
            maker.centerY.equalTo(left1)
        }
        
        left2.snp.makeConstraints { (maker) in
            maker.left.equalTo(8)
            maker.top.equalTo(right1.snp.bottom).offset(12)
            maker.bottom.equalToSuperview().offset(-16)
        }
        
        right2.snp.makeConstraints { (maker) in
            maker.right.equalTo(right1)
            maker.centerY.equalTo(left2)
        }
        
//        statusLabel.snp.makeConstraints { (maker) in
//            maker.right.equalTo(right2.snp.left).offset(-8)
//            maker.centerY.equalTo(left2)
//            maker.height.equalTo(25)
//        }
    }
    
    lazy var bgView: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor.white
        return view
    } ()
    
    lazy var noLabel: UILabel = {
        let label = UILabel()
        label.textColor = UIColor.white
        label.font = UIFont.boldSystemFont(ofSize: 15)
        return label
    } ()
    
    lazy var deleteButton: UIButton = {
        let btn = UIButton()
        btn.setImage(#imageLiteral(resourceName: "del"), for: UIControl.State())
        return btn
    } ()
    
    lazy var left1: UILabel = {
        let label = UILabel()
        label.textColor = Theme.textColor
        label.font = UIFont.systemFont(ofSize: 15)
        return label
    } ()
    
    lazy var right1: UILabel = {
        let label = UILabel()
        label.textColor = Theme.textColor
        label.font = UIFont.systemFont(ofSize: 15)
        return label
    } ()
    
    lazy var left2: UILabel = {
        let label = UILabel()
        label.textColor = Theme.textColor
        label.font = UIFont.systemFont(ofSize: 15)
        return label
    } ()
    
    lazy var statusLabel: UILabel = {
        let label = UILabel()
        label.backgroundColor = Theme.tableViewColor
        label.layer.cornerRadius = 5
        label.layer.masksToBounds = true
        label.textAlignment = .center
        label.textColor = Theme.blue
        label.font = UIFont.systemFont(ofSize: 14)
        return label
    } ()
    
    lazy var right2: UILabel = {
        let label = UILabel()
        label.backgroundColor = Theme.tableViewColor
        label.layer.cornerRadius = 5
        label.textColor = Theme.blue
        label.layer.masksToBounds = true
        label.textAlignment = .center
        label.font = UIFont.systemFont(ofSize: 14)
        return label
    } ()
    
    lazy var subview: UIView = {
        let view = UIView()
        view.backgroundColor = Theme.primaryLightColor
        return view
    } ()
    
}
